package com.leapp.sk.ui.activity.im

import android.os.Bundle
import android.support.v4.app.Fragment
import android.text.TextUtils
import cc.vv.lkbasecomponent.http.LKHttp
import cc.vv.lkbasecomponent.http.lib.setting.Settings
import cc.vv.lkbasecomponent.util.LKSPUtil
import cc.vv.lkimcomponent.lkim.bean.LKIMMessage
import cc.vv.lklibrary.http.base.okhttpfactory.plugins.httpplugin.MediaTypeWrap
import cc.vv.lklibrary.log.LogOperate
import com.example.mvp.activity.BaseActivityMVP
import com.example.mvp.binder.BaseDataBinder
import com.leapp.sk.R
import com.example.mvp.bean.ClassMemberInfoEntity
import com.example.mvp.bean.GroupMemberEntity
import com.example.util.ObjectProcessingUtil
import com.leapp.sk.app.GET_CLASS_MEMBER
import com.leapp.sk.bean.MemberAllUserDataEntity
import com.leapp.sk.bean.TopLayoutTitleEntity
import com.leapp.sk.bean.request.MemberAllUserListRequest
import com.leapp.sk.bean.response.MemberAllUserListResponse
import com.leapp.sk.binder.BaseBinder
import com.leapp.sk.ui.fragment.message.ReadMessageFragment
import com.leapp.sk.ui.fragment.message.UnReadMessageFragment
import com.example.util.finallist.*
import com.leapp.sk.viewdelegate.im.MessageStateDelegate

/**
 * 消息已读未读状态展示界面
 * @author  chaohaipeng  on 2018/7/26.
 */
class MessageStateActivity : BaseActivityMVP<MessageStateDelegate>() {

    /**
     * fragment数据集合
     */
    private var fragmentLists = mutableListOf<Fragment>()
    /**
     * 标签集合
     */
    private var titles = mutableListOf<TopLayoutTitleEntity>()
    /**
     * 消息体数据
     */
    private var mLKIMMessage:LKIMMessage?=null


    override fun initView(p0: Bundle?) {
        viewDelegate.setTitle(resources.getString(R.string.string_message_title))

    }

    override fun initDate() {

//        LogOperate.e("已读成员Id==" + readMemberIds)
//        LogOperate.e("未读成员Id==" + unReadMemberIds)

        mLKIMMessage = intent?.getParcelableExtra<LKIMMessage>(MSG_DATA)

        val groupId = intent?.getStringExtra(GROUP_ID)
        getHttpData(groupId)
    }

    override fun bindEvenListener() {




    }

    override fun getDelegateClass(): Class<MessageStateDelegate> {
        return MessageStateDelegate::class.java

    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return BaseBinder()
    }



    private fun getHttpData(groupId:String?) {
        if(groupId==null){
            return
        }
        val memberAllUserListRequest = MemberAllUserListRequest()
        memberAllUserListRequest.classIdOrGroupId = groupId
        memberAllUserListRequest.type = 2
        LKHttp.post(GET_CLASS_MEMBER, memberAllUserListRequest, MemberAllUserListResponse::class.java, object : BaseCallBack<MemberAllUserListResponse>(this) {
            override fun onSuccess(url: String?, resultText: String?, obj: MemberAllUserListResponse?) {
                super.onSuccess(url, resultText, obj)
                if (obj?.code == 200) {
                    val classDetialDataEntity: MemberAllUserDataEntity = ObjectProcessingUtil.objMandatoryConversionClass(obj?.data, MemberAllUserDataEntity::class.java)
                    val classMsg = classDetialDataEntity?.classMsg
                    if (classMsg != null) {
                        getdata(classMsg)
                    }
                }

            }

            override fun onFailure(url: String?, isLoading: Boolean, exceptionStr: String?) {
                super.onFailure(url, isLoading, exceptionStr)
                LogOperate.e("返回结果===" + exceptionStr)
            }
        }
                , true, Settings(MediaTypeWrap.MEDIA_TYPE_FORM))
    }

    /**
     * 数据处理
     */
    fun getdata(classMsg:List<ClassMemberInfoEntity>){
        var isRead=false
        var readMessage = ArrayList<GroupMemberEntity>()
        var unReadMessage = ArrayList<GroupMemberEntity>()
        val stringId = LKSPUtil.getInstance().getString(KEY_USERID)
        val readMemberIds = intent?.getStringExtra(READ_MESSAGE_IDS)
        val unReadMemberIds = intent?.getStringExtra(UNREAD_MESSAGE_IDS)
        val split = readMemberIds?.split(",")
        for (i in classMsg.indices) {
            val classMsgEntity = GroupMemberEntity()
            val get = classMsg.get(i)
            if (!TextUtils.equals(stringId,get.id)) {

                if (split != null) {
                    for (i in split.indices) {
                        if (TextUtils.equals(get.phone, split.get(i))) {
                            classMsgEntity.avatar = get.avatar
                            classMsgEntity.nickName = get.nickName
                            classMsgEntity.phone = get.phone
                            readMessage.add(classMsgEntity)
                            isRead = true
                            continue
                        } else {
                            isRead = false
                            continue
                        }
                    }
                    if (!isRead) {
                        classMsgEntity.avatar = get.avatar
                        classMsgEntity.nickName = get.nickName
                        classMsgEntity.phone = get.phone
                        unReadMessage.add(classMsgEntity)
                    }
                } else {
                    classMsgEntity.avatar = get.avatar
                    classMsgEntity.nickName = get.nickName
                    classMsgEntity.phone = get.phone
                    unReadMessage.add(classMsgEntity)
                }
            }
        }
        val unReadMessageFragment = UnReadMessageFragment()
        val bundleUn = Bundle()
        bundleUn.putParcelableArrayList(UNREAD_MESSAGE_DATAS, unReadMessage)
        bundleUn.putParcelable(MSG_DATA,mLKIMMessage)
        unReadMessageFragment.arguments = bundleUn

        val readMessageFragment = ReadMessageFragment()
        val bundle = Bundle()
        bundle.putParcelableArrayList(READ_MESSAGE_DATAS, readMessage)
        readMessageFragment.arguments = bundle
        fragmentLists.add(unReadMessageFragment)
        fragmentLists.add(readMessageFragment)
        val topULayoutTitleEntity = TopLayoutTitleEntity()
        topULayoutTitleEntity.title = String.format(resources.getString(R.string.string_message_unread), unReadMessage.size)
        val topRLayoutTitleEntity = TopLayoutTitleEntity()
        topRLayoutTitleEntity.title = String.format(resources.getString(R.string.string_message_read), readMessage.size)
        titles.add(topULayoutTitleEntity)
        titles.add(topRLayoutTitleEntity)
        viewDelegate.initTabLayout(this, fragmentLists, titles)
    }


}